package com.xsm.activitygroup;
import java.util.ArrayList;

import com.xsm.adapter.List1Adapter;
import com.xsm.object.CTextObject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FragmentB  extends Fragment{
	private Classification context;
	private ArrayList<CTextObject> arrayListB;
	private ArrayAdapter<String> adapter;
	private ArrayList<String> valuesB;
	private boolean select[];
	private CTextObject selectATextObject;
	private CTextObject selectBTextObject;
	private ArrayList<CTextObject> selectBList;
	private ListView  listview;
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragmentb, container,false);
	}
	
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		context=(Classification)getActivity();
		final FragmentC fragmentC=(FragmentC)getFragmentManager().findFragmentById(R.id.fragment3);
		arrayListB=(ArrayList<CTextObject>)context.getIntent().getExtras().get("arrayListB");
		selectBList=new ArrayList<CTextObject>();
		valuesB=new ArrayList<String>();	
		final FragmentA fragmenta=(FragmentA)getFragmentManager().findFragmentById(R.id.fragment1);
		selectATextObject=fragmenta.getObject();
        updateList(selectATextObject);
		listview=(ListView)context.findViewById(R.id.listb);
	     adapter=new ArrayAdapter<String>(context,R.layout.listtext,valuesB);
		listview.setAdapter(adapter);
		
		listview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
			context.OpenDrawer();
			selectBTextObject=selectBList.get(position);
			fragmentC.UpdateCList(selectBTextObject);
			fragmentC.Settext(fragmenta.getObject().getName()+"-"+selectBTextObject.getName());
			}
		});
		selectBTextObject=selectBList.get(0);
	}
	
	public void UpdateSelectList(CTextObject object){
	  updateList(object);
	   listview.setAdapter(adapter);
	}
	
	private void updateList(CTextObject object){
		 String id=object.getTextid();
		 valuesB.clear();
		 selectBList.clear();
		 for(int i=0;i<arrayListB.size();i++){
		 if(arrayListB.get(i).getParentid().equals(id)){
		 valuesB.add(arrayListB.get(i).getName());
		 selectBList.add(arrayListB.get(i));
		 }
		}
	}
	public CTextObject getBTextObject(){
		return selectBTextObject;
	}
	
}
